﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Quizzer.Model;

namespace Quizzer.DataAccess
{
    public static class TestRepository
    {
        readonly static Dictionary<Guid, Test> _tests;
        readonly static Dictionary<Guid, AccountTest> _accountTests;

        static TestRepository()
        {
            _tests = LoadTests();
            _accountTests = LoadAccountTests();
        }
        static Dictionary<Guid, Test> LoadTests()
        {
            return Test.Items;
        }
        static Dictionary<Guid, AccountTest> LoadAccountTests()
        {
            return AccountTest.Items;
        }

        public static ObservableCollection<Test> GetTestsOfCourse(Course course)
        {
            var query = from test in _tests.Values
                        where test.CourseID == course.Id
                        select test;

            return new ObservableCollection<Test>(query.ToList());
        }

        public static ObservableCollection<Test> GetTestsForUser(Account account, Course course)
        {
            var query = from ta in _accountTests.Values
                        where (ta.AccountID == account.Id) && (_tests[ta.Id].CourseID == course.Id) && (ta.IsAccessible == true)
                        select _tests[ta.Id];

            return new ObservableCollection<Test>(query.ToList());
        }

        public static int GetNumberOfQuestionFoTest(Test test)
        {
            return _tests[test.Id].NumberOfQuestions;
        }
    }
}
